#include "stdafx.h"
#include "ScreenTemplate.h"

using namespace MomogenkyouSystem;

ScreenTemplate::ScreenTemplate()
:	//m_pScreenManager(NULL),
	m_ScreenState(TransitionOn),
	m_bIsExiting(false), m_bFocusedOtherScreen(false), m_bIsPopup(false),
	m_fTransitionOnTime(0),	m_fTransitionOffTime(0), m_fTransitionPosition(1.0f),
	IsModal(true)
{
}

ScreenTemplate::~ScreenTemplate()
{
}


void	ScreenTemplate::Initialize()
{
}

void	ScreenTemplate::Shutdown()
{
}

void	ScreenTemplate::Update(const float & fInterval)
{
	UNREFERENCED_PARAMETER(fInterval);
}

void	ScreenTemplate::ProcessInput()
{
}

void	ScreenTemplate::DrawGameScreen(const float & fInterval)
{
	UNREFERENCED_PARAMETER(fInterval);
}

void	ScreenTemplate::UpdateScreen(const float & fInterval, const bool & bFocusOther, const bool & bCoveredByOther)
{
	m_bFocusedOtherScreen	=	bFocusOther;
	if(m_bIsExiting)
	{
		m_ScreenState	=	TransitionOff;

		if(!UpdateTransition(fInterval, m_fTransitionOffTime, 1))
		{
			 // When the transition finishes, remove the screen.
			//m_pScreenManager->RemoveScreen(this);
			ScreenManager::RemoveScreen(this);
		}
	}
	else if (bCoveredByOther)
	{
		// If the screen is covered by another, it should transition off.
		if(UpdateTransition(fInterval, m_fTransitionOffTime, 1))
			m_ScreenState = TransitionOff;	//	Still Moving
		else	
			m_ScreenState = Hidden;			//	Transition Finished
	}
	else
	{
		// Otherwise the screen should transition on and become active.
		if (UpdateTransition (fInterval, m_fTransitionOffTime, -1))
			m_ScreenState = TransitionOn;	//	Still Moving
		else	
			m_ScreenState = Active;			//	Transition Finished
	}
	Update(fInterval);
}

bool	ScreenTemplate::UpdateTransition(const float & fInterval, const float & fElapsedTime, const int & nDirection)
{
	//	Amout to move
	float	_TransitionDelta(0);
	if(fElapsedTime == 0)
		_TransitionDelta = 1.0f;
	else
		_TransitionDelta = fInterval / fElapsedTime;

	//	Apply Movement to the ratio
	m_fTransitionPosition += _TransitionDelta * nDirection;
	
	//	Check End of Transisition
	if ((nDirection < 0 && m_fTransitionPosition <= 0) ||
		(nDirection > 0 && m_fTransitionPosition >= 1))
	{
		MomogenkyouHelper::Clamp(0, 1.0f, m_fTransitionPosition, m_fTransitionPosition);
		return false;
	}
	return	true;
}

void	ScreenTemplate::ExitScreen()
{
	if(m_fTransitionOffTime == 0.0f)
	{
		// If the screen has a zero transition time, remove it immediately.
		//m_pScreenManager->RemoveScreen(this);
		ScreenManager::RemoveScreen(this);
	}
	else
	{
		m_bIsExiting = true;
	}
}